package pl.edu.pw.ee.buildcity.scm.subversion;

import org.fest.assertions.Fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import pl.edu.pw.ee.buildcity.commons.Const;
import pl.edu.pw.ee.buildcity.scm.SCMException;

import java.io.File;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;

/**
 * @author mgrzechocinski
 * @since 2009-10-10
 */
@RunWith(MockitoJUnitRunner.class)
public class SubversionClientImplTestCase {

	@Mock
	private SVNClientManager clientManager;

	@Test
	public void shouldCatchAnyExceptionWhileCheckoutingProject() {
		//Given
		given(clientManager.getUpdateClient()).willThrow(new NullPointerException("error msg"));
		SubversionServiceImpl subversionServiceImpl = new SubversionServiceImpl(clientManager);

		//When
		try {
			SVNCheckoutParameters parameters = SVNCheckoutParameters.checkoutHEAD("http://myhost.com/any/path");
			subversionServiceImpl.checkout(parameters, Const.WORKING_COPIES);
			Fail.fail("Exception should be thrown");
		} catch (SCMException e) {
			//Then
			assertThat(e.getMessage()).isEqualTo("SCM exception occured: error msg");
			assertThat(e.getCause()).isInstanceOf(NullPointerException.class);

		}
		verify(clientManager).getUpdateClient();
	}

	@Test
	public void shouldCatchAnyExceptionWhileCheckingIfChangeWasDetected() {
		//Given
		given(clientManager.getWCClient()).willThrow(new NullPointerException("error msg"));
		SubversionServiceImpl subversionServiceImpl = new SubversionServiceImpl(clientManager);

		//When
		try {
			subversionServiceImpl.changeDetected(new File("/any/path"));
			Fail.fail("Exception should be thrown");
		} catch (SCMException e) {
			//Then
			assertThat(e.getMessage()).isEqualTo("SCM exception occured: error msg");
			assertThat(e.getCause()).isInstanceOf(NullPointerException.class);

		}
		verify(clientManager).getWCClient();
	}

	@Test
	public void shouldCatchAnyExceptionWhileUpdatingToHEAD() {
		//Given
		given(clientManager.getUpdateClient()).willThrow(new NullPointerException("error msg"));
		SubversionServiceImpl subversionServiceImpl = new SubversionServiceImpl(clientManager);

		//When
		try {
			subversionServiceImpl.updateToHEAD(new File("/any/path"));
			Fail.fail("Exception should be thrown");
		} catch (SCMException e) {
			//Then
			assertThat(e.getMessage()).isEqualTo("SCM exception occured: error msg");
			assertThat(e.getCause()).isInstanceOf(NullPointerException.class);

		}
		verify(clientManager).getUpdateClient();
	}

	@Test
	public void shouldCatchAnyExceptionWhileGettingLogEntries() {
		//Given
		given(clientManager.getLogClient()).willThrow(new NullPointerException("error msg"));
		SubversionServiceImpl subversionServiceImpl = new SubversionServiceImpl(clientManager);

		//When
		try {
			subversionServiceImpl.getLogBetweenHEADAndWorkingCopyRevision(new File("/any/path"));
			Fail.fail("Exception should be thrown");
		} catch (SCMException e) {
			//Then
			assertThat(e.getMessage()).isEqualTo("SCM exception occured: error msg");
			assertThat(e.getCause()).isInstanceOf(NullPointerException.class);

		}
		verify(clientManager).getLogClient();
	}
}
